package cqu.example.cacluator;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import java.util.HashMap;
import java.util.Map;
import java.util.Stack;
import java.util.Vector;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private String num1="";
    private String num2="";
    private String result="";
    private String text="0";
    Stack<String> stack=new Stack<>();

    private TextView showtext;

    HashMap<String,Integer>mp=new HashMap<>();
    private TextView showtext1;
    private TextView showtext2;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        stack.push("0");
        mp.put("+",1);
        mp.put("-",1);
        mp.put("×",2);
        mp.put("÷",2);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        showtext = findViewById(R.id.res);
        showtext1 = findViewById(R.id.res1);
        showtext2 = findViewById(R.id.res2);

        //数字
        Button zero=findViewById(R.id.zero);
        Button one=findViewById(R.id.one);
        Button two=findViewById(R.id.two);
        Button three=findViewById(R.id.third);
        Button four=findViewById(R.id.four);
        Button five=findViewById(R.id.five);
        Button six=findViewById(R.id.six);
        Button seven=findViewById(R.id.seven);
        Button eight=findViewById(R.id.eight);
        Button nine=findViewById(R.id.nine);
        zero.setOnClickListener(this);
        one.setOnClickListener(this);
        two.setOnClickListener(this);
        three.setOnClickListener(this);
        four.setOnClickListener(this);
        five.setOnClickListener(this);
        six.setOnClickListener(this);
        seven.setOnClickListener(this);
        eight.setOnClickListener(this);
        nine.setOnClickListener(this);
        //运算
        Button AC=findViewById(R.id.AC);
        Button CE=findViewById(R.id.CE);
        Button present=findViewById(R.id.present);
        ImageButton sqrt=findViewById(R.id.sqrt);
        Button multiply=findViewById(R.id.mul);
        Button div=findViewById(R.id.div);
        Button plus=findViewById(R.id.plus);
        Button sub=findViewById(R.id.sub);
        Button dot=findViewById(R.id.dot);
        Button equal=findViewById(R.id.equal);
        AC.setOnClickListener(this);
        CE.setOnClickListener(this);
        present.setOnClickListener(this);
        sqrt.setOnClickListener(this);
        multiply.setOnClickListener(this);
        div.setOnClickListener(this);
        plus.setOnClickListener(this);
        sub.setOnClickListener(this);
        dot.setOnClickListener(this);
        equal.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {

        if(!stack.empty())
            showtext2.setText("运行栈："+stack);

        if(view.getId()== R.id.zero){
            if(!text.equals("0")) {
                text = text + "0";
                stack.push("0");
                refresh(text);
            }

        }

        else if (view.getId()==R.id.CE) {//删除
            if(text!=""&&!stack.empty()){
            text=text.substring(0,text.length()-1);
            stack.pop();
          }
            refresh(text);

        }
        else if (view.getId()==R.id.AC) {//归零
            text="0";
            stack.clear();
            stack.push("0");
            refresh(text);

        }

        else if(view.getId()==R.id.sqrt){//
            if(!isNum()){
                text=text+"√";
                stack.push("√");
            } else if (text.equals("0")){
                stack.pop();
                text="√";
                stack.push("√");
            }
            refresh(text);
        }
        else if(view.getId()==R.id.present){//
            if(!isop()){
                text=text+"%";
                stack.push("%");
                refresh(text);
            }
            refresh(text);
        }
        else if (view.getId()==R.id.mul) {
            if(!isop()&&!stack.empty()){
                text=text+"×";
                stack.push("×");
                refresh(text);
            }
        } else if (view.getId()==R.id.div) {
            if(!isop()&&!stack.empty()) {
                text = text + "÷";
                stack.push("÷");
                refresh(text);
            }
        } else if (view.getId()==R.id.plus) {
            if(!isop()&&!stack.empty()) {
                text = text + "+";
                stack.push("+");
                refresh(text);
            }

        } else if (view.getId()==R.id.sub) {
            if(!isop()&&!stack.empty()){
            text = text + "-";
            stack.push("-");
            refresh(text);}

        }
        else if (view.getId()==R.id.dot) {

            text = text + ".";
            stack.push(".");
            refresh(text);

        }else if (view.getId()==R.id.equal) {
            if (!stack.empty()) {
                caculator();
            }
        }
         else{//这里只用来加数字
            if(text.equals("0")) {text=((TextView) view).getText().toString();
                stack.pop();
                stack.push(((TextView) view).getText().toString());}
            else
            {text=text+((TextView) view).getText().toString();//将这个按钮中的内容取出
            stack.push(((TextView) view).getText().toString());}
            refresh(text);

        }

    }
    private boolean isNum(){
        if(stack.empty())return false;
       return(stack.peek().equals("0")||stack.peek().equals("1")||stack.peek().equals("2")||stack.peek().equals("3")||stack.peek().equals("4")||stack.peek().equals("5")||stack.peek().equals("6")||stack.peek().equals("7")||stack.peek().equals("8")||stack.peek().equals("9"));
    }
    private boolean isop(){
        if(stack.empty())return false;
        return (stack.peek().equals(".")||stack.peek().equals("√")||stack.peek().equals("×")||stack.peek().equals("÷")||stack.peek().equals("+")||stack.peek().equals("-"));
    }
    private void refresh(String temp){
        showtext.setText(temp);
    }
    private  void caculator(){
        String num="";
        String s="";
        String op="";
        Stack<String> mid=new Stack<>();
        Stack<String>Op=new Stack<>();
        Stack<String>Num=new Stack<>();//存放后缀表达式
        while(!stack.empty()){
            if(stack.peek().equals("+")||stack.peek().equals("-")||stack.peek().equals("×")||stack.peek().equals("÷"))
            { mid.push(s);
                mid.push(stack.pop());
                s="";
            }
            else s=stack.pop()+s;

        }//从这个栈中我可以按照顺序将字符取出
        if(!s.isEmpty())mid.push(s);
        showtext2.setText(""+mid);


        while(!mid.empty()){
            if(mid.peek().equals("+")||mid.peek().equals("-")||mid.peek().equals("×")||mid.peek().equals("÷")){
                if(Op.empty())Op.push(mid.pop());//符号栈为空直接压栈
                else{
                    if(mp.get(mid.peek())>mp.get(Op.peek())){Op.push(mid.pop());}
                    else{
                        while(mp.get(mid.peek())<=mp.get(Op.peek()))
                        {Num.push(Op.pop());
                            if(Op.empty())break;
                        }

                    }
                }

            }
            else Num.push(mid.pop());
        }
        while(!Op.empty())Num.push(Op.pop());
        //现在得到了后缀表达式的栈
        showtext1.setText("后缀表达式："+Num);



        Stack<String> res=new Stack<>();
        Stack<String>newNum=new Stack<>();
        while(!Num.empty()){
            newNum.push(Num.pop());
        }

        String temp1,temp2;
        while(!newNum.empty()){
            switch (newNum.peek()){//当为运算符那么弹出两个数字进行运算
                case "+":{res.push(""+(Double.parseDouble(res.pop())+Double.parseDouble(res.pop())));
                    newNum.pop();
                    break;}
                case "-":{
                    temp1=res.pop();
                    temp2=res.pop();
                    res.push(""+(Double.parseDouble(temp2)-Double.parseDouble(temp1)));
                    newNum.pop();
                    break;}
                case "×":{res.push(""+(Double.parseDouble(res.pop())*Double.parseDouble(res.pop())));
                    newNum.pop();
                    break;}
                case "÷":{
                    temp1=res.pop();
                    temp2=res.pop();
                    res.push(""+(Double.parseDouble(temp2)/Double.parseDouble(temp1)));
                    newNum.pop();
                    break;}
                default:    {if(newNum.peek().substring(newNum.peek().length()-1).equals("%")&&newNum.peek().substring(0,1).equals("√")){
                    //用来判断是否首尾为根号和百分号
                    res.push(""+Math.sqrt(Double.parseDouble(newNum.peek().substring(1,newNum.peek().length()-1)))/100);
                    newNum.pop();
                }//判断是否尾部百分号
                    else if(newNum.peek().substring(newNum.peek().length()-1).equals("%"))
                {res.push(""+Double.parseDouble(newNum.peek().substring(0,newNum.peek().length()-1))/100);
                    newNum.pop();

                } else if (newNum.peek().substring(0,1).equals("√")) {//判断是否首尾为根号
                    res.push(""+Math.sqrt(Double.parseDouble(newNum.peek().substring(1))));
                    newNum.pop();
                }else{//单纯的数字直接压入结果栈中
                    res.push(newNum.pop());
                }
                break;}
            }
        }
        if(!stack.empty()){
            stack.clear();
        }

        for(int i=0;i<=res.peek().length()-1;i++){
            stack.push(""+res.peek().charAt(i));
        }
        showtext2.setText("栈："+stack);
        text=res.pop();
        refresh(text);

  }
}